//
//  LLTabBar.m
//  LLRiseTabBarDemo
//
//  Created by Meilbn on 10/18/15.
//  Copyright © 2015 meilbn. All rights reserved.
//

#import "LLTabBar.h"

//#import "SWLoginTableViewController.h"

//#import "UIView+SWView.h"

@interface LLTabBar ()

//@property (strong, nonatomic) NSMutableArray *tabBarItems;

@end

@implementation LLTabBar

#pragma mark - Lifecycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        [self config];
    }
    
    return self;
}

#pragma mark - Private Method

- (void)config {
    self.backgroundColor = [UIColor gb_naviBarBackgroundcolor];
    UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, -0.5, Main_Screen_Width, .5)];
    topLine.image = [UIImage imageNamed:@"tapbar_top_line"];
    topLine.backgroundColor = UIColorFromRGB(0xD4D4D4);
    [self addSubview:topLine];
}

- (void)setSelectedIndex:(NSInteger)index {
    
    for (LLTabBarItem *item in self.tabBarItems) {
        if (item.tag == index) {
            item.selected = YES;
        } else {
            item.selected = NO;
        }
    }
    
    //由于该方法只能获取rootViewCOntroller，
    //该项目中有tabBarController 嵌套 tabBarController,
    //故改用下面方法获取tabBar所在的控制器为tabBarController
//    UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
//    UITabBarController *tabBarController = (UITabBarController *)keyWindow.rootViewController;
    UITabBarController *tabBarController = (UITabBarController *)[self viewController];
    if (tabBarController) {
        tabBarController.selectedIndex = index;
    }
    
    
}

#pragma mark - Touch Event

- (void)itemSelected:(LLTabBarItem *)sender {
    
    if (sender.needDoSomeThing) {
        //这里是需要做的某些事,就是没有登录情况下推出登录界面
        if (![Utiles isLogin]) {
            SWLoginTableViewController *loginVC = [SWLoginTableViewController dd_instanceFromStoryboard];
            
            [[[Utiles getCurrentVC] navigationController] showViewController:loginVC sender:nil];
            return;
        }
    }
    
    if (sender.tabBarItemType != LLTabBarItemRise) {
        [self setSelectedIndex:sender.tag];
    } else {
        if (self.delegate) {
            if ([self.delegate respondsToSelector:@selector(tabBarDidSelectedRiseButton)]) {
                [self.delegate tabBarDidSelectedRiseButton];
            }
        }
    }
}

#pragma mark - Setter

- (void)setTabBarItemAttributes:(NSArray<NSDictionary *> *)tabBarItemAttributes {
    _tabBarItemAttributes = tabBarItemAttributes.copy;
    
    NSAssert(_tabBarItemAttributes.count > 2, @"TabBar item count must great than two.");
    
//    CGFloat normalItemWidth = (WIDTH(self) * 3 / 4) / (_tabBarItemAttributes.count - 1);
//    CGFloat tabBarHeight = CGRectGetHeight(self.frame);
//    CGFloat publishItemWidth = (WIDTH(self) / 4);
    
    //这里是让所有的tababr一样的宽度，不修改下面逻辑代码的情况下，只是修改了这里的normalItemWidth=publishItemWidth
    //这样如果有特定需求可以快速修改，不需要重修写逻辑代码
    CGFloat normalItemWidth = WIDTH(self)/_tabBarItemAttributes.count;
    CGFloat tabBarHeight = CGRectGetHeight(self.frame);
    CGFloat publishItemWidth = WIDTH(self)/_tabBarItemAttributes.count;

    
    NSInteger itemTag = 0;
    BOOL passedRiseItem = NO;
    
    _tabBarItems = [NSMutableArray arrayWithCapacity:_tabBarItemAttributes.count];
    
    for (id item in _tabBarItemAttributes) {
        if ([item isKindOfClass:[NSDictionary class]]) {
            NSDictionary *itemDict = (NSDictionary *)item;
            
            LLTabBarItemType type = [itemDict[kLLTabBarItemAttributeType] integerValue];
            
            CGRect frame = CGRectMake(itemTag * normalItemWidth + (passedRiseItem ? publishItemWidth : 0), 0, type == LLTabBarItemRise ? publishItemWidth : normalItemWidth, tabBarHeight);
            
            LLTabBarItem *tabBarItem = [self tabBarItemWithFrame:frame
                                                           title:itemDict[kLLTabBarItemAttributeTitle]
                                                 normalImageName:itemDict[kLLTabBarItemAttributeNormalImageName]
                                               selectedImageName:itemDict[kLLTabBarItemAttributeSelectedImageName] tabBarItemType:type
                                        needDoSomeThing:[itemDict[kLLTabBarItemAttributeNeedDoSomeThing] boolValue]];
            if (itemTag == 0) {
                tabBarItem.selected = YES;
            }
            
            [tabBarItem addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
            
            if (tabBarItem.tabBarItemType != LLTabBarItemRise) {
                tabBarItem.tag = itemTag;
                itemTag++;
            } else {
                passedRiseItem = YES;
            }
            
            [_tabBarItems addObject:tabBarItem];
            [self addSubview:tabBarItem];
        }
    }
}

- (LLTabBarItem *)tabBarItemWithFrame:(CGRect)frame title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName tabBarItemType:(LLTabBarItemType)tabBarItemType needDoSomeThing:(BOOL)needDoSomeThing{
    LLTabBarItem *item = [[LLTabBarItem alloc] initWithFrame:frame];
    [item setTitle:title forState:UIControlStateNormal];
    [item setTitle:title forState:UIControlStateSelected];
    item.titleLabel.font = [UIFont systemFontOfSize:10];
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    [item setImage:normalImage forState:UIControlStateNormal];
    [item setImage:selectedImage forState:UIControlStateSelected];
    [item setTitleColor:[UIColor gb_textColor_5d5f6a] forState:UIControlStateNormal];
    [item setTitleColor:[UIColor mainBodyColor] forState:UIControlStateSelected];
    item.tabBarItemType = tabBarItemType;
    item.needDoSomeThing = needDoSomeThing;
    return item;
}

@end
